#include "passwordmodiydialog.h"
#include "ui_passwordmodiydialog.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"
#include <QRegExpValidator>

//#include <QDebug>

PasswordModiyDialog::PasswordModiyDialog(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PasswordModiyDialog)
{
    ui->setupUi(this);

    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif

    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
    this->setStyleSheet("outline: none;");            //无虚线框

    //ui->Guest_Btn->setEnabled(false);
    //ui->Admin_Btn->setEnabled(false);

    QRegExp regx("[a-zA-Z0-9]{0,6}");
    QValidator *validator = new QRegExpValidator(regx);
    ui->newPass_lineEdit->setValidator( validator );
    ui->newPass_lineEdit->setValidator( validator );
}

void PasswordModiyDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->Guest_Btn->setGeometry(50,0,367,101);
        ui->Admin_Btn->setGeometry(50,120,367,101);

        ui->label->setGeometry(55,250,100,30);
        ui->label_2->setGeometry(55,300,100,30);
        ui->label_3->setGeometry(55,350,130,30);

        ui->oldPass_lineEdit->setGeometry(190,250,210,30);
        ui->newPass_lineEdit->setGeometry(190,300,210,30);
        ui->newPassTwo_lineEdit->setGeometry(190,350,210,30);

        ui->confirm_Btn->setGeometry(185,400,60,35);
        ui->cancel_Btn->setGeometry(340,400,60,35);

        break;
    default:
        break;
    }
}

PasswordModiyDialog::~PasswordModiyDialog()
{
    delete ui;
}

void PasswordModiyDialog::on_cancel_Btn_clicked()
{
    this->hide();
}

void PasswordModiyDialog::updateView()
{
    //qDebug()<<"usertype";
    if(changeAccount==EM_ACCOUNT_USER)
    {
        ui->Guest_Btn->setDisabled(true);
        ui->Admin_Btn->setDisabled(false);
    }
    else
    {
        ui->Guest_Btn->setDisabled(false);
        ui->Admin_Btn->setDisabled(true);
    }
}

void PasswordModiyDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void PasswordModiyDialog::on_Guest_Btn_clicked()
{
    //if(changeAccount==EM_ACCOUNT_ADMIN)
    {
        changeAccount = EM_ACCOUNT_USER;
        ui->Guest_Btn->setDisabled(true);
        ui->Admin_Btn->setDisabled(false);

        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("修改操作员密码\n不需要输入原密码"));
    }
    //else
    {
    }
}

void PasswordModiyDialog::on_Admin_Btn_clicked()
{
    //if(changeAccount==EM_ACCOUNT_USER)
    {
        changeAccount = EM_ACCOUNT_ADMIN;
        ui->Guest_Btn->setDisabled(false);
        ui->Admin_Btn->setDisabled(true);
    }
    //else
    {
    }
}

void PasswordModiyDialog::on_confirm_Btn_clicked()
{
    //qDebug()<<"eject";
    if(messagePrompt())
    {
        emit modifySignal();
    }
}

bool PasswordModiyDialog::messagePrompt()
{
    //qDebug()<<"ok";
    if(EM_ACCOUNT_USER == userAccount)
    {
        //qDebug()<<"insufficient";
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("没有权限\n只有管理员才能修改密码"));
        return false;
    }
    else
    {
        if(EM_ACCOUNT_ADMIN == changeAccount)
        {
            if(ui->oldPass_lineEdit->text().isEmpty()||ui->newPass_lineEdit->text().isEmpty()||ui->newPassTwo_lineEdit->text().isEmpty())
            {
                //qDebug()<<"empty";
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("密码不能为空"));
                return false;
            }
            else if(ui->oldPass_lineEdit->text() != userPassword)
            {
                //qDebug()<<"false";
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("原密码不正确"));
                return false;
            }
        }


        //else
        {
            if(ui->oldPass_lineEdit->text() == ui->newPass_lineEdit->text())
            {
                //qDebug()<<"error1";
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("不能与原密码相同"));
                return false;
            }
            else if(ui->newPass_lineEdit->text() != ui->newPassTwo_lineEdit->text())
            {
                //qDebug()<<"error2";
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("两次输入密码不一致"));
                return false;
            }
            else
            {
                newPassword = ui->newPassTwo_lineEdit->text();
                return true;
                //qDebug()<<"success";
                //emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("密码修改成功"));
            }
        }
    }
}
